﻿namespace Ops.Host.Core.Services.Impl;

internal sealed class DvSpotCheckService : IDvSpotCheckService
{
    private readonly SqlSugarRepository<DvSpotCheck> _spotcheckRepo;

    public DvSpotCheckService(SqlSugarRepository<DvSpotCheck> spotcheckRepo)
    {
        _spotcheckRepo = spotcheckRepo;
    }

    public async Task AddSpotCheckAsync(DvSpotCheck data)
    {
        await _spotcheckRepo.InsertAsync(data);
    }

    public async Task<PagedList<DvSpotCheck>> GetPagedListAsync(SpotCheckFilter filter, int pageIndex, int pageSize)
    {
        return await _spotcheckRepo.AsQueryable()
                .WhereIF(!string.IsNullOrWhiteSpace(filter.Station), s => s.StationCode == filter.Station)
                .WhereIF(!string.IsNullOrWhiteSpace(filter.Tooling), s => s.Tooling == filter.Tooling)
                .WhereIF(!string.IsNullOrWhiteSpace(filter.Item), s => s.Item == filter.Item)
                .WhereIF(filter.IsPass != null, s => s.IsPass == filter.IsPass)
                .WhereIF(!string.IsNullOrEmpty(filter.Shift), s => s.Shift == filter.Shift)
                .WhereIF(filter.CreateTimeStart != null, s => s.CreateTime >= filter.CreateTimeStart.ToDayMin())
                .WhereIF(filter.CreateTimeEnd != null, s => s.CreateTime <= filter.CreateTimeEnd.ToDayMax())
                .OrderByDescending(s => s.CreateTime)
                .ToPagedListAsync(pageIndex, pageSize);
    }
}
